From d5a7bc33d280c0cb13e6ac277d6dc521fe8b0c91 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 27 May 2014 18:53:30 -0700 Subject: [PATCH] Improve shell and add tests --- src/cargo/core/shell.rs | 3 +-- tests/test_shell.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_shell.rs diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index ea4fb0602..7bcdbddd6 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -3,7 +3,6 @@ use term::{Terminal,color}; use term::color::Color; use term::attr::Attr; use std::io::IoResult; -use std::io::stdio::StdWriter; pub struct ShellConfig { pub color: bool, @@ -23,7 +22,7 @@ pub struct Shell { impl Shell { pub fn create(out: T, config: ShellConfig) -> Option> { - if config.tty { + if config.tty && config.color { let term: Option> = Terminal::new(out); term.map(|t| Shell { terminal: Color(box t as Box>), config: config }) } else { diff --git a/tests/test_shell.rs b/tests/test_shell.rs new file mode 100644 index 000000000..a4e591265 --- /dev/null +++ b/tests/test_shell.rs @@ -0,0 +1,40 @@ +use support::{ResultTest,Tap,shell_writes}; +use hamcrest::{assert_that}; +use std::io::{MemWriter,IoResult}; +use std::str::from_utf8_lossy; +use cargo::core::shell::{Shell,ShellConfig}; +use term::{Terminal,TerminfoTerminal,color}; + +fn setup() { +} + +test!(non_tty { + Shell::create(MemWriter::new(), ShellConfig { color: true, verbose: true, tty: false }).assert().tap(|shell| { + shell.say("Hey Alex", color::RED).assert(); + assert_that(shell, shell_writes("Hey Alex\n")); + }); +}) + +test!(color_explicitly_disabled { + Shell::create(MemWriter::new(), ShellConfig { color: false, verbose: true, tty: true }).assert().tap(|shell| { + shell.say("Hey Alex", color::RED).assert(); + assert_that(shell, shell_writes("Hey Alex\n")); + }); +}) + +test!(colored_shell { + Shell::create(MemWriter::new(), ShellConfig { color: true, verbose: true, tty: true }).assert().tap(|shell| { + shell.say("Hey Alex", color::RED).assert(); + assert_that(shell, shell_writes(colored_output("Hey Alex\n", color::RED).assert())); + }); +}) + +fn colored_output(string: S, color: color::Color) -> IoResult { + let mut term: TerminfoTerminal = Terminal::new(MemWriter::new()).assert(); + try!(term.reset()); + try!(term.fg(color)); + try!(term.write_str(string.as_slice())); + try!(term.reset()); + try!(term.flush()); + Ok(from_utf8_lossy(term.get_ref().get_ref()).to_str()) +} -- 2.30.2